package JavaBeans;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class Validate {

	public static boolean checkUser(String username, String pass) {
		if (username == null || username.trim().length() == 0) {
			return false;
		}
		
		return realValidate(username,pass);
		
	}

	private static boolean realValidate(String username, String pass) {
		boolean st = false;
		Connection con = null;
		PreparedStatement ps  = null;
		ResultSet rs  = null;
		try {

			Class.forName("org.sqlite.JDBC");

			// creating connection with the database
			con = DriverManager.getConnection(
					"jdbc:sqlite:/demo.db");
			ps = con.prepareStatement("select * from users where Id=? and password=?");
			ps.setString(1, username);
			ps.setString(2, pass);
			rs = ps.executeQuery();
			st = rs.next();

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if(rs!=null)
					rs.close();
				if(ps!=null)
					ps.close();
				if(con!=null)
					con.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return st;
	}

}

